package com.dongdongshop.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.dongdongshop.data.ItemCatResule;
import com.dongdongshop.data.Result;
import com.dongdongshop.pojo.itemCat.TbItemCat;
import com.dongdongshop.service.itemCat.ItemCatService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("itemCat")
public class ItemCatController {
    @Reference
    private ItemCatService itemCatService;
    private final Logger logger = LoggerFactory.getLogger(ItemCatController.class);
    @RequestMapping("itemCatShow")
    private String toItemCatShow(){
        return "admin/item_cat";
    }
    //查询分类数据
    @RequestMapping("listItemCat")
    @ResponseBody
    private ItemCatResule listItemCat(@RequestParam(defaultValue = "0")Long parentId){
        try {
            List<TbItemCat> parentList = itemCatService.listParent(parentId);
            List<TbItemCat> listItemCat = itemCatService.listItemCat(parentId);
            return ItemCatResule.ok().setData(listItemCat).setParentData(parentList);

        }catch (Exception e){
            logger.error("查询错误",e);
            return ItemCatResule.fail();
        }
    }
    //添加分类数据错误
    @RequestMapping("insertItemCat")
    @ResponseBody
    public Result insertItemCat(TbItemCat tbItemCat){
        try {
            int i = itemCatService.insertItemCat(tbItemCat);
            return Result.ok();

        }catch (Exception e){
            return Result.fail();
        }
    }
    //修改数据回显
    @RequestMapping("getItemCat")
    @ResponseBody
    public Result getItemCat(Long id){
        try {
            TbItemCat tbItemCat = itemCatService.getItemCat(id);
            return Result.ok().setInfo(tbItemCat);
        }catch (Exception e){
            logger.error("修改回显数据错误",e);
            return Result.fail();
        }
    }
    //修改数据
    @RequestMapping("updateItemCat")
    @ResponseBody
    public Result updateItemCat(TbItemCat tbItemCat){
        try {
            int i = itemCatService.updateItemCat(tbItemCat);
            return Result.ok();
        }catch (Exception e){
            logger.error("修改数据错误",e);
            return Result.fail();
        }
    }
    @RequestMapping("deleteItemCatBatch")
    @ResponseBody
    public Result deleteItemCatBatch(@RequestParam("ids[]")Long[] ids){
        try {
            int i = itemCatService.deleteItemCatBatch(ids);
            return Result.ok();
        }catch (Exception e){
            logger.error("删除数据错误",e);
            return Result.ok();
        }
    }
}
